package com.woniuxy.woniumall.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Or;
import com.woniuxy.woniumall.entity.Order;
import com.woniuxy.woniumall.entity.OrderItem;
import com.woniuxy.woniumall.entity.User;
import com.woniuxy.woniumall.mapper.BookMapper;
import com.woniuxy.woniumall.mapper.CartMapper;
import com.woniuxy.woniumall.mapper.OrderItemMapper;
import com.woniuxy.woniumall.mapper.OrderMapper;
import com.woniuxy.woniumall.vo.CartVo;
import com.woniuxy.woniumall.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class OrderServcie {
    @Autowired
    CartMapper cartMapper ;

    @Autowired
    BookMapper bookMapper ;

    @Autowired
    OrderMapper orderMapper ;

    @Autowired
    OrderItemMapper orderItemMapper ;

    @Transactional
    public void submitOrder(Integer aid, Integer[] ids, User user) {
        // 0 ， 把购物车中的数据全部查询出来
        List<CartVo> cartVos = cartMapper.selectSubmitCart(user.getId(), ids);
        double total = 0.0 ;
        //1. 减库存  使用乐观锁
        for (CartVo cv :cartVos) {
            int i = bookMapper.updateStoreCount(cv.getBookVo().getId(),cv.getBuycount());
            if (i < 1) {
                throw new RuntimeException("图书【"+cv.getBookVo().getBname()+"】库存不足，只有【"+cv.getBookVo().getStorecount()+"】本!");
            }
            total += cv.getAllprice();
        }
        //2.保存订单
        /*
        private String ordernum;
        private Date createdate;
        private Integer userId;
        private Integer addressId;
        private Double totalprice;
        private Integer status;
         */
        Order order = new Order();
        order.setOrdernum("WONIU"+System.currentTimeMillis()+"");
        order.setCreatedate(new Date());
        order.setAddressId(aid);
        order.setUserId(user.getId());
        order.setStatus(1);
        order.setTotalprice(total);
        //保存时 一定要返回当前的id 值
        orderMapper.insert(order);
        //3 。保存明细
        /*
        private Integer bookId;
        private Integer buycount;
        private Integer orderId;
        private Double allprice;
         */
        for (CartVo cv :cartVos) {
            OrderItem item = new OrderItem();
            item.setAllprice(cv.getAllprice());
            item.setOrderId(order.getId());
            item.setBookId(cv.getBookVo().getId());
            item.setBuycount(cv.getBuycount());
            //保存
            orderItemMapper.insert(item);

            // 4 .删除购物车中的数据
            cartMapper.deleteByPrimaryKey(cv.getId());
        }
    }

    public PageInfo<OrderVo> findOrderByPage(int userid, String name, Integer now, Integer size) {
        PageHelper.startPage(now,size);
        List<OrderVo> lists = orderMapper.selectOrderByUser(name,userid);
        return new PageInfo<>(lists) ;
    }
}
